package com.liyunc.demo.bundle.security.config;

import com.liyunc.demo.bundle.security.service.AuthenticationDataLoaderService;
import com.liyunc.demo.bundle.security.service.AuthenticatorService;
import com.liyunc.demo.comp.auth.AuthenticationDataLoader;
import com.liyunc.demo.comp.auth.Authenticator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AnonymousAuthenticationProvider;
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter;

import java.util.UUID;

/**
 * @author liyuncong
 * @version 1.0
 * @file AuthBeansConfiguration.java
 * @brief AuthBeansConfiguration
 * @details AuthBeansConfiguration
 * @date 2022-03-31
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2022-03-31     liyuncong          Created
 */
@Configuration
public class AuthBeansConfiguration {
    public static final String ANONYMOUS_AUTH_KEY = "guest_" + UUID.randomUUID();

    @Bean
    @ConditionalOnMissingBean(AuthenticationDataLoader.class)
    public AuthenticationDataLoaderService authenticationDataLoaderService() {
        return new AuthenticationDataLoaderService();
    }

    @Bean
    @ConditionalOnMissingBean(Authenticator.class)
    public AuthenticatorService<Object> authenticatorService() {
        return new AuthenticatorService<>();
    }

    @Bean
    public AnonymousAuthenticationProvider authenticationProvider() {
        return new AnonymousAuthenticationProvider(ANONYMOUS_AUTH_KEY);
    }

    @Bean
    public AnonymousAuthenticationFilter anonymousAuthenticationFilter() {
        return new AnonymousAuthenticationFilter(ANONYMOUS_AUTH_KEY);
    }
}
